<?php

include_once('global.php');
include_once('session.php');
include_once('dom.php');
include_once('cover.php');
include_once('messages.php');
include_once('options.php');




//--------------------------------------------------------------------------------
// Show the calendar page
//--------------------------------------------------------------------------------
function showCalendar() {
  global $dom;
  debug("Showing calendars page");
  $xroot = $dom->documentElement;
  $xcalendars = $dom->createElement("calendars");
  $xroot->appendChild($xcalendars);

  //$xcalendars->appendChild(addCalendar(6, "June", 3, 30));
  $xcalendars->appendChild(addCalendar(7, "July", 5, 31));
  $xcalendars->appendChild(addCalendar(8, "August", 1, 31));
  $xcalendars->appendChild(addCalendar(9, "September", 4, 30));
  $xcalendars->appendChild(addCalendar(10, "October", 6, 13));
}

// Load all events for some month
function loadEvents($month) {
  $events = array();
  $query = "SELECT dayofmonth(event_ts), name, description FROM events ";
  $query .= " WHERE month(event_ts)=$month";
  debugSQL($query);
  $rs = mysql_query( $query );
  if( $rs > 0 ) {
    while( list($day, $name, $desc) = mysql_fetch_row ($rs) ) {
      if( $events[$day] == null )
         $events[$day] = array();
      //else
        $events[$day][] = $name;
    }
  }
  return $events;
}

function addCalendar($month, $monthname, $skip, $days) {
  global $dom;
  $events = loadEvents($month);
  $xcalendar = $dom->createElement("calendar");
  $xcalendar->setAttribute("title", "$monthname Jumping Events");
  $newweek = 0;
  $day = 1;
  $xweek = null;
  while( $day <= $days or $newweek != 0 ) {
    if( $newweek == 0 ) {
      $xweek = $dom->createElement("week");
      $xcalendar->appendChild($xweek);
      $newweek = 7;
    }
    $xday = $dom->createElement("day");
    $xweek->appendChild($xday);
    if( $skip > 0 )
      $skip = $skip-1;
    else {
      if( $day <= $days ) {
        $xday->setAttribute("day", $day);
        if( $events[$day] != null ) {
          foreach( $events[$day] as $name ) {
            $xevent = $dom->createElement("event");
            $xevent->setAttribute("name", $name);
            $xday->appendChild($xevent);
          }
        }
      }
      $day = $day+1;
    }
    $newweek--;
  }
  return $xcalendar;
}

?>
